package com.mrd.serverconnect.util;

import android.app.Application;
import android.content.Context;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.LocaleList;
import android.util.DisplayMetrics;

import java.util.Locale;

public class LanguageUtil {

    public   static final String CHINESE_SIMPLE_IDENTIFIER  = "zh-CN";
    public   static final String CHINESE_TRADITIONAL_IDENTIFIER  = "zh-TW";
    public   static final String ENGLISH_IDENTIFIER  = "en";
    public   static final String JAPANESE_IDENTIFIER  = "ja-JP";

    private static final String APPNAME = "com.mrd.serverconnect";
    public static final String ERROR_LABEL = "";


    /**
     * 更新resource
     * @param resource
     * @param l
     * @return
     */
    private static Resources updateResource(Resources resource, Locale l) {
        Configuration config = resource.getConfiguration();
        config.locale = l;
        DisplayMetrics dm = resource.getDisplayMetrics();
        resource.updateConfiguration(config, dm);
        return resource;
    }

    /**
     * 获取resource
     * @param pm
     * @param pkgName
     * @param locale
     * @return
     */
    private static Resources getApplicationResource(PackageManager pm, String pkgName, Locale locale) {
        Resources resourceForApplication = null;
        try {
            resourceForApplication = pm.getResourcesForApplication(pkgName);
            updateResource(resourceForApplication, locale);
        } catch (PackageManager.NameNotFoundException e) {

        }
        return resourceForApplication;
    }

    /**
     * 获取对应语言的String
     * @param context
     * @param stringId
     * @param locale
     * @return
     */
    public static String getStringByLocale(Context context, int stringId, Locale locale) {
        Resources resources;
        if(locale!=null){
            resources = getApplicationResource(context.getApplicationContext().getPackageManager(),
                    APPNAME, locale);
        }else{

            resources = getApplicationResource(context.getApplicationContext().getPackageManager(),
                    APPNAME, getSetLanguageLocale(context,SPUtils.getString(context,SPUtils.LAST_LANGUAGE_IDENTIFIER)));
        }

        if (resources == null) {
            return ERROR_LABEL;
        } else {
            try {
                return resources.getString(stringId);
            } catch (Exception e) {
                return ERROR_LABEL;
            }
        }
    }

    /**
     * 切换语言
     * @param context
     * @param languageIdentifier
     * @return
     */
    public static Resources toggleLanguage(Context context,String languageIdentifier) {
        Locale locale = getSetLanguageLocale(context,languageIdentifier);
        Resources resources = getApplicationResource(context.getApplicationContext().getPackageManager(),APPNAME,locale);
        return updateResource(resources,locale);
    }

    /**
     * 获取选择的语言设置
     *
     * @param context
     * @return
     */
    public static Locale getSetLanguageLocale(Context context,String languageIdentifier) {

        // 没有存过
        if(languageIdentifier==null||languageIdentifier.length()==0){
            return getSystemLocale(context);
        }
        switch (languageIdentifier) {
            case CHINESE_SIMPLE_IDENTIFIER:
                return Locale.SIMPLIFIED_CHINESE;
            case CHINESE_TRADITIONAL_IDENTIFIER:
                return Locale.TRADITIONAL_CHINESE;
            case ENGLISH_IDENTIFIER:
                return Locale.ENGLISH;
            case JAPANESE_IDENTIFIER:
                return Locale.JAPANESE;
            default:
                return Locale.ENGLISH;
        }
    }

    /**
     * 获取系统的locale
     *
     * @return Locale对象
     */
    public static Locale getSystemLocale(Context context) {
        Locale locale;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            locale = LocaleList.getDefault().get(0);
        } else {
            locale = Locale.getDefault();
        }
        return locale;
    }


}
